//
//  BaseViewModel.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/6/5.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class BaseViewModel: NSObject {
    
    
    
}

// MARK: - 网络请求主方法
extension BaseViewModel {
    
    /// 发起get请求
    func getReq(path: String,
                params: Parameters? = nil,
                encoding: ParameterEncoding = URLEncoding.default,
                headers: [String: String]? = nil,
                respData: Bool = false,
                handler: @escaping BKResultHandler) {
        self.mainReq(method: .get, path: path, params: params, encoding: encoding, headers: headers, respData: respData, handler: handler)
    }
    
    /// 发起post请求
    func postReq(path: String,
                 params: Parameters? = nil,
                 encoding: ParameterEncoding = JSONEncoding.default,
                 headers: [String: String]? = nil,
                 respData: Bool = false,
                 handler: @escaping BKResultHandler) {
        self.mainReq(method: .post, path: path, params: params, encoding: encoding, headers: headers, respData: respData, handler: handler)
    }
    
    /** 请求主方法*/
    func mainReq(method: HTTPMethod,
                 path: String,
                 params: Parameters? = nil,
                 encoding: ParameterEncoding = URLEncoding.default,
                 headers: [String: String]? = nil,
                 respData: Bool = false,
                 handler: @escaping BKResultHandler) {
        
        var reqHeaders = NetTool.baseHeaders()
        if let xmHeader = headers {
            xmHeader.forEach { reqHeaders.add(name: $0.key, value: $0.value) }
        }
        NetTool.default.req(method: method, path: path, params: params, encoding: encoding, headers: reqHeaders, respData: respData, success: { (result) in
            
            if !respData {
                let json = JSON(result as Any)
                let ok = NetTool.success(json)
                let msg = json[NetMessageKey].stringValue
                let code = json[NetStatusKey].intValue
                let statusCode = NetStatusCode(rawValue: code) ?? .reqTimeOut
                if statusCode == .tokenInvalid && AppHelper.token.isValid {
                    AppHelper.clearAllUserCache()
                    TaskTool.delay(0.5) {
                        guard let window = UIApplication.shared.mainWindow() else { return }
                        AppHelper.startLogin(window)
                    }
                }
                
                NetTool.log(path: path, params: params, headers: reqHeaders, json: json)
                handler(ok, json, msg, statusCode)
            } else {
                handler(true, result, nil, .reqSuccess)
            }
            
        }) { (error, msg, code)  in
            NetTool.log(path: path, params: params, headers: reqHeaders, json: JSON(msg as Any))
            handler(false, error, msg, code)
        }
    }
    
}

// MARK: - 网络请求
extension BaseViewModel {
    
    /// 获取用户信息-手机端
    func req_userInfo(handler: @escaping BKResultHandler) {
        self.getReq(path: NetApiPort.User.userInfo, handler: handler)
    }
    
}
